package gameOfLife;

import interfaces.Cell;
import interfaces.World;

import lib.Location;


/**
 * 
 * The Alive Cell implementation in the Conway's Game of Life
 */
public class AliveCell implements Cell{
	LocationHelperImpl locHelper = new LocationHelperImpl();
	
	/**
	 * The transform rule for a alive Cell
	 */
	public Cell transform(World w, Location loc) {
		Location[] neighbors;
		if(w.is2D()){
			neighbors =  locHelper.getNeighbors(loc,w.getDimension(),w.getDimension());
		}else{
			neighbors =  locHelper.getNeighbors(loc,w.getDimension(),1);
		}
		
		int numAdjacent = 0;
		

		for(Location n : neighbors) {
			if(Math.abs(loc.getX() - n.getX()) <= 1 &&
					Math.abs(loc.getX() - n.getX()) <= 1){
				if(w.getCell(n) instanceof AliveCell)
					numAdjacent++;
			}
		}
		
		if(numAdjacent < 4 && numAdjacent > 1)
			return new AliveCell();
		else 
			return new DeadCell();
		
	
	}
	/**
	 * Get the name of this type of Cell
	 */
	public String getName(){
		return "AliveCell";
	}



}
